//
//  FFPayOrder.m
//  FFMall
//
//  Created by ale tan on 2017/9/21.
//  Copyright © 2017年 IT.Bear. All rights reserved.
//

#import "FFPayOrder.h"

#import "FFPayInfo.h"
#import "FFPayAlertView.h"
#import "FFPayTypeView.h"

//请求
#import "GetOrderRequest.h"
#import "FBPayTypeModel.h"

#import "GetPayInfoRequest.h"

#import "FFWebViewController.h"

@interface FFPayOrder ()

@property (nonatomic ,strong) PayOrder payOrderBlock;

@property (nonatomic ,strong)FBPayTypeModel *choosePay;
@property (nonatomic ,strong) PayResult payResultBlock;

@end

@implementation FFPayOrder

#pragma mark -Order-下单
- (void)payOrderWithProducts:(NSArray *)products Infos:(NSDictionary *)infos Complete:(PayOrder)payOrder{
    _payOrderBlock = payOrder;
    
//    FFWebViewController *webVC = (FFWebViewController *)[UIApplication sharedApplication].keyWindow.rootViewController;
//    [webVC goLogin];
    
    [self getPreOrderWithProduct:products Infos:infos];
}

- (void)getPreOrderWithProduct:(NSArray *)products Infos:(NSDictionary *)infos{
    GetOrderRequest *request = [GetOrderRequest new];
    [request createSetterWithProducts:products Address:nil Shipping:nil Complete:^(id responseDic, BOOL result) {
        
        if (result) {
            FBOrderModel *orderModel = (FBOrderModel *)responseDic;
            FFPayOrderView *payOrderView = [FFPayOrderView orderViewWithInfos:infos Model:orderModel];
            [payOrderView setPayOrder:^(FBOrderModel *model, BOOL success) {
                
                //下单成功选择支付
                [self payNowWithOrder:model ViewController:[UIApplication sharedApplication].keyWindow.rootViewController PayResult:^(BOOL success) {
                    if (_payOrderBlock) {
                        _payOrderBlock(model,success);
                    }
                    
                    if (!success) {
                        [FFTipsView showTips:@"订单已生成，可在订单列表中继续支付"];
                    }
                }];
            }];
            [payOrderView show];
        }
    }];
}

#pragma mark -Pay-支付
//支付
- (void)payNowWithOrder:(FBOrderModel *)order ViewController:(UIViewController *)viewController PayResult:(PayResult)payResult
{
     __block FFPayTypeView *payTypeView = [FFPayTypeView payTypeViewModel:order ChoosePayType:^(FBPayTypeModel *typeModel) {
        
        if (typeModel) {
            
            self.choosePay = typeModel;
            
            GetPayInfoRequest *request = [GetPayInfoRequest new];
            [request getSignWithOrderNo:order.orderNo PayMethod:typeModel Complete:^(id responseDic, BOOL result) {
                if (result) {
                    FFPayInfo * payInfo = [[FFPayInfo alloc] init];
                    payInfo.paymentID = typeModel.payMethodId;
                    payInfo.orderId = order.orderNo;
                    payInfo.info = responseDic;
                    FFPayManager *payManager = [[FFPayManager alloc] init];
                    [payManager goPayWithPayInfo:payInfo payResultBlock:^(BOOL success) {
                        if (success) {
                            [FFTipsView showTips:@"已付款成功！"];
                            if (payResult) {
                                payResult(YES);
                            }
                            [payTypeView removeFromSuperview];
                        }
                        else{
                            [FFTipsView showTips:@"付款失败！"];
                            if (payResult) {
                                payResult(NO);
                            }
                        }
                        
                    } controller:nil];
                    
                    [payTypeView removeFromSuperview];
                }
                
                //取消支付
                else{
                    [FFTipsView showTips:responseDic[@"toast"]];
//                    if (payResult) {
//                        payResult(NO);
//                    }
                }
            }];
        }
        //
        else{
            if (payResult) {
                payResult(NO);
            }
            [payTypeView removeFromSuperview];
            NSLog(@"用户不选择支付");
        }
    }];
    [payTypeView show];
}

//取消订单
- (void)goCancleOrder:(FBOrderModel *)order Section:(NSInteger)section Button:(UIButton *)button{
    _payResultBlock(NO);
}

- (void)dismissPayView:(UITapGestureRecognizer *)tap
{
    UIView *containView = tap.view;
    //    UIView *payView = [containView viewWithTag:111];
    [containView removeFromSuperview];
}

@end
